package com.hisense.hiioc.hismartplate.controller;

import com.hisense.hiioc.hismartplate.base.BaseController;
import com.hisense.hiioc.hismartplate.constants.URLConstant;
import com.hisense.hiioc.hismartplate.entity.response.BaseRes;
import com.hisense.hiioc.hismartplate.entity.table.ClientSearchEntity;
import com.hisense.hiioc.hismartplate.entity.table.FaceEventEntity;
import com.hisense.hiioc.hismartplate.entity.table.query.FaceIdRangeQuery;
import com.hisense.hiioc.hismartplate.service.ClientSearchUrlService;
import com.hisense.hiioc.hismartplate.service.FaceEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: almo
 * @Date: 2018/12/11 上午9:05
 * @Version 1.0
 */
@RestController
@RequestMapping(URLConstant.ClientSearchURLMapping.BASE_URL)
public class ClientSearchController extends BaseController<ClientSearchUrlService> {

    @Autowired
    private FaceEventService faceEventService;

    @PostMapping("/clientClickEvent")
    public BaseRes uploadFaceClientClickEvent(@RequestBody ClientSearchEntity entity) {
        FaceEventEntity newEntity = faceEventService.getNewestFaceEventByDevice(entity.getDeviceID());

        if (newEntity != null && newEntity.getFaceOut() == null && newEntity.getFaceID() != null) {
            entity.setFaceID(newEntity.getFaceID());
        }

        int result = defaultService.uploadClientSearchUrl(entity);
        return generateHttpOkRes(entity);
    }

    @PostMapping("/clientFaceInEvent")
    public BaseRes insertFaceInClientEvent(@RequestBody FaceEventEntity entity) {
        int result = faceEventService.insertFaceInClientEvent(entity);
        return generateHttpOkRes(entity);
    }

    @PostMapping("/clientFaceOutEvent")
    public BaseRes updateFaceOutClientEvent(@RequestBody FaceEventEntity entity) {
        FaceEventEntity newEntity = faceEventService.getNewestFaceEventByDevice(entity.getDeviceID());
        newEntity.setFaceOut(entity.getFaceOut());
        int result = faceEventService.updateFaceOutClientEvent(newEntity);
        return generateHttpOkRes(newEntity);
    }

    @PostMapping("/faceCloudIdEvent")
    public BaseRes updateFaceIdCloudEvent(@RequestBody FaceEventEntity entity) {

        FaceEventEntity newEntity = faceEventService.getNewestFaceEventByDevice(entity.getDeviceID());

        newEntity.setFaceTime(entity.getFaceTime());
        newEntity.setFaceID(entity.getFaceID());

        int result = faceEventService.updateFaceIdCloudEvent(newEntity);

        List<ClientSearchEntity> clientSearchEntities = defaultService.getClientEventByTimeRange(newEntity);

        if(clientSearchEntities!=null&&clientSearchEntities.size()>0) {
            FaceIdRangeQuery rangeQuery = new FaceIdRangeQuery();
            rangeQuery.setFaceEventEntity(newEntity);
            rangeQuery.setSearchEntityList(clientSearchEntities);
            defaultService.updateClientEventFaceId(rangeQuery);
        }
//        Map<String,Object> map = new HashMap<>();
//        map.put("FACE_ID","face_09");
//        map.put("FACE_TIME",entity.getFaceTime());
//        faceEventService.upda teByMap(map);
        return generateHttpOkRes(entity);
    }

    @PostMapping("/clientSearchHistory")
    public BaseRes getClientSearchHistory(@RequestBody ClientSearchEntity entity) {

        FaceEventEntity faceEventEntity = faceEventService.getNewestFaceEventByDevice(entity.getDeviceID());
        ClientSearchEntity result = null;

        if(faceEventEntity!=null&&faceEventEntity.getFaceOut()==null&&faceEventEntity.getFaceID()!=null) {
            result = defaultService.getClientEventHistoryById(faceEventEntity);
        }
        return generateHttpOkRes(result);
    }

}
